home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / plugins / mapmakextree.py < prev    next >
Text File  |  2004-01-05  |  13KB  |  365 lines

  1. Info = {
  2.   "plug-in":       "Make a simple tree",
  3.   "desc":          "Make an tree from several intersecting brushes",
  4.   "date":          "2003.04.20",
  5.   "author":        "Marco 'NerdIII' Leise",
  6.   "author e-mail": "Marco.Leise@gmx.de",
  7.   "quark":         "Version 6.x"
  8. }
  9.  
  10. from math import sin
  11. from math import cos
  12. from math import pi
  13. import quarkx
  14. import maptagside
  15. import quarkpy.qmacro
  16. import quarkpy.qtoolbar
  17. from quarkpy.maputils import *
  18.  
  19. #base class for all games
  20. class MakeXTreeDlg(quarkpy.qmacro.dialogbox):
  21.   size = (300, 180)
  22.   dfsep = 0.4     # separation at 40% between labels and edit boxes
  23.   dlgflags = FWF_KEEPFOCUS + FWF_NORESIZE
  24.   dlgdef = """
  25.     {
  26.       Style = "13"
  27.       Caption = "simple tree maker 1.1"
  28.       tex: = {
  29.         Txt = "Tree texture:"
  30.         Typ = "ET"
  31.         SelectMe = "1"
  32.         Hint = "Use transparent textures for trees"
  33.       }
  34.       planes: = {
  35.         Txt = "Number of 'wings'"
  36.         Typ = "EF1"
  37.         Min = '3'
  38.         Max = '32'
  39.       }
  40.       scale: = {
  41.         Txt = "Scale tree by"
  42.         Typ = "EF1"
  43.         Min = '0.1'
  44.         Hint = "This will add a size factor to the usual texturesize of the tree."
  45.       }
  46.     """
  47.  
  48.   def __init__(self, form, editor):
  49.     self.editor = editor
  50.     src = quarkx.newobj(":")
  51.  
  52.     # Based on the textures in the selections, initialize the from and to textures
  53.     texlist = quarkx.texturesof(editor.layout.explorer.sellist)
  54.     if len(texlist) == 1:
  55.       src["tex"] = texlist[0]
  56.     else:
  57.       src["tex"] = quarkx.setupsubset()["DefaultTexture"]
  58.     src["planes"]      = 4,
  59.     src["scale"]       = 1,
  60.     src["alternate"]     = "X"
  61.     src["useblue"]     = ""
  62.  
  63.     # Create the dialog form and the buttons
  64.     quarkpy.qmacro.dialogbox.__init__(self, form, src,
  65.       close = quarkpy.qtoolbar.button(
  66.         self.close,
  67.         "close this box",
  68.         ico_editor, 0,
  69.         "Close"),
  70.       MakeXTree = quarkpy.qtoolbar.button(
  71.         self.MakeXTree,
  72.         "make tree",
  73.         ico_editor, 3,
  74.         "Make Tree"))
  75.  
  76.   def AcceptDialog(self):
  77.     # Commit any pending changes in the dialog box
  78.     quarkx.globalaccept()
  79.     # Gather information about what is to be created
  80.     self.tex     = self.src["tex"]
  81.     self.planes  = int((self.src["planes"])[0])
  82.     self.scale   = (self.src["scale"])[0]
  83.     self.blue    = self.src["useblue"] is not None
  84.     self.alternate = self.src["alternate"] is not None
  85.     texobj = quarkx.loadtexture (self.tex, self.editor.TexSource)
  86.     if texobj is not None:
  87.       try:
  88.         texobj = texobj.disktexture
  89.       except quarkx.error:
  90.         texobj = None
  91.     if texobj is not None:
  92.       self.tsize = texobj ["size"]
  93.     else:
  94.       self.tsize = (128.0,128.0)
  95.     self.twidth  = self.tsize[0]*self.scale
  96.     self.theight = self.tsize[1]*self.scale
  97.     self.treename = "huge tree"
  98.     if self.theight <= 192: self.treename = "tree"
  99.     if self.theight <= 64: self.treename = "bush"
  100.     if self.theight <= 32: self.treename = "grass"
  101.  
  102. class MakeHLXTreeDlg(MakeXTreeDlg):
  103.   dlgdef = MakeXTreeDlg.dlgdef + """
  104.         alternate: = {
  105.           Typ = "X"
  106.           Txt = "Switch texture"
  107.           Cap = "orientation on every wing"
  108.           Hint = "On gives more variety from the chosen texture but should not be used on trees with bent trunks."
  109.         }
  110.         useblue: = {
  111.           Typ = "X"
  112.           Txt = "No null texture"
  113.           Cap = "Only use the '{BLUE' texture instead"
  114.           Hint = "Only use {BLUE if your set of build tools is too old. Using the null texture will increase performance."
  115.         }
  116.         sep: = { Typ ="S" Txt=""}
  117.         MakeXTree:py = {Txt="" }
  118.         close:py = {Txt="" }
  119.       }
  120.     """
  121.  
  122.   def MakeXTree(self, btn):
  123.     self.AcceptDialog()
  124.     if self.blue:
  125.       nulltex = "{BLUE"
  126.     else:
  127.       nulltex = "NULL"
  128.     # Create the tree
  129.     g = quarkx.newobj(self.treename+":g");
  130.     aface = quarkx.newobj("null:f")
  131.     aface["v"] = (-self.twidth/2,-self.twidth/2,self.theight, self.twidth/2,-self.twidth/2,self.theight, -self.twidth/2,self.twidth/2,self.theight)
  132.     aface["tex"] = nulltex
  133.     g.appenditem(aface)
  134.     aface = quarkx.newobj("null:f")
  135.     aface["v"] = (-self.twidth/2,-self.twidth/2,0, -self.twidth/2,self.twidth/2,0, self.twidth/2,-self.twidth/2,0)
  136.     aface["tex"] = nulltex
  137.     g.appenditem(aface)
  138.        
  139.     ang=pi*2/self.planes
  140.     addang=pi/self.planes/2
  141.     addlen=sin(addang)/cos(addang)*self.twidth/2
  142.     for j in range(2):
  143.       e = quarkx.newobj("func_illusionary:b");
  144.       e["rendermode"] = "4"
  145.       e["renderamt"] = "127"
  146.       g.appenditem(e)
  147.       for i in range(self.planes):
  148.         p = quarkx.newobj("tree-side:p");
  149.         aface = quarkx.newobj("tree:f")
  150.         sx1=cos(ang*i)*self.twidth/2
  151.         sy1=sin(ang*i)*self.twidth/2
  152.         if j==0:
  153.           aface["v"] = (0,0,0, sx1,sy1,0, 0,0,self.theight)
  154.         else:
  155.           aface["v"] = (0,0,0, 0,0,self.theight, sx1,sy1,0)
  156.         aface["tex"] = self.tex
  157.         if (i>=self.planes/2) ^ ((self.alternate) & (i % 2==1)) ^ j==1:
  158.           sign= 1
  159.         else:
  160.           sign=-1
  161.         aface["tv"] = (-self.twidth/2,0,
  162.                         sign*self.twidth / (self.tsize[0] / 128)-self.twidth/2,0,
  163.                        -self.twidth/2, -self.theight / (self.tsize[1] / 128) - 0.5)
  164.         p.appenditem(aface)
  165.         aface = quarkx.newobj("null:f")
  166.         if j==0:
  167.           sx2=sx1-sin(ang*i)*addlen
  168.           sy2=sy1+cos(ang*i)*addlen
  169.           aface["v"] = (0,0,0, 0,0,self.theight, sx2,sy2,0)
  170.         else:
  171.           sx2=sx1+sin(ang*i)*addlen
  172.           sy2=sy1-cos(ang*i)*addlen
  173.           aface["v"] = (0,0,0, sx2,sy2,0, 0,0,self.theight)
  174.         aface["tex"] = nulltex
  175.         p.appenditem(aface)
  176.         aface = quarkx.newobj("null:f")
  177.         if j==0:
  178.           aface["v"] = (sx1,sy1,0, sx2,sy2,0, sx2,sy2,self.theight)
  179.         else:
  180.           aface["v"] = (sx1,sy1,0, sx2,sy2,self.theight, sx2,sy2,0)
  181.         aface["tex"] = nulltex
  182.         p.appenditem(aface)
  183.         e.appenditem(p)
  184.     # Drop the items
  185.     quarkpy.mapbtns.dropitemsnow(self.editor, [g], "make x-tree")
  186.     
  187. class MakeSinXTreeDlg(MakeXTreeDlg):
  188.   dlgdef = MakeXTreeDlg.dlgdef + """
  189.         alternate: = {
  190.           Typ = "X"
  191.           Txt = "Switch texture"
  192.           Cap = "orientation on every wing"
  193.           Hint = "On gives more variety from the chosen texture but should not be used on trees with bent trunks."
  194.         }
  195.         sep: = { Typ ="S" Txt=""}
  196.         MakeXTree:py = {Txt="" }
  197.         close:py = {Txt="" }
  198.       }
  199.     """
  200.  
  201.   def MakeFace(self,type):
  202.     if type == 1:
  203.       aface = quarkx.newobj("tree:f")
  204.       aface["tex"] = self.tex
  205.     else:
  206.       aface = quarkx.newobj("null:f")
  207.       aface["tex"] = self.nulltex
  208.     aface["Contents"] = "402653248"
  209.     aface["Flags"] = "2"
  210.     return aface
  211.  
  212.   def MakeXTree(self, btn):
  213.     self.AcceptDialog()
  214.     self.nulltex = "generic/misc/skip"
  215.     # Create the tree
  216.     g = quarkx.newobj(self.treename+":g");
  217.     aface = self.MakeFace(0)
  218.     aface["v"] = (-self.twidth/2,-self.twidth/2,self.theight, self.twidth/2,-self.twidth/2,self.theight, -self.twidth/2,self.twidth/2,self.theight)
  219.     g.appenditem(aface)
  220.     aface = self.MakeFace(0)
  221.     aface["v"] = (-self.twidth/2,-self.twidth/2,0, -self.twidth/2,self.twidth/2,0, self.twidth/2,-self.twidth/2,0)
  222.     g.appenditem(aface)
  223.     
  224.     ang=pi*2/self.planes
  225.     addang=pi/self.planes/2
  226.     addlen=sin(addang)/cos(addang)*self.twidth/2
  227.     for j in range(2):
  228.       e = quarkx.newobj("func_illusionary:b");
  229.       g.appenditem(e)
  230.       for i in range(self.planes):
  231.         p = quarkx.newobj("tree-side:p");
  232.         aface = self.MakeFace(1)
  233.         sx1=cos(ang*i)*self.twidth/2
  234.         sy1=sin(ang*i)*self.twidth/2
  235.         if j==0:
  236.           aface["v"] = (0,0,0, sx1,sy1,0, 0,0,self.theight)
  237.         else:
  238.           aface["v"] = (0,0,0, 0,0,self.theight, sx1,sy1,0)
  239.         if (i>=self.planes/2) ^ ((self.alternate) & (i % 2==1)) ^ j==1:
  240.           sign= 1
  241.         else:
  242.           sign=-1
  243.         aface["tv"] = (-self.twidth/2,0,
  244.                         sign*self.twidth / (self.tsize[0] / 128)-self.twidth/2,0,
  245.                        -self.twidth/2, -self.theight / (self.tsize[1] / 128) - 0.5)
  246.         p.appenditem(aface)
  247.         aface = self.MakeFace(0)
  248.         if j==0:
  249.           sx2=sx1-sin(ang*i)*addlen
  250.           sy2=sy1+cos(ang*i)*addlen
  251.           aface["v"] = (0,0,0, 0,0,self.theight, sx2,sy2,0)
  252.         else:
  253.           sx2=sx1+sin(ang*i)*addlen
  254.           sy2=sy1-cos(ang*i)*addlen
  255.           aface["v"] = (0,0,0, sx2,sy2,0, 0,0,self.theight)
  256.         p.appenditem(aface)
  257.         aface = self.MakeFace(0)
  258.         if j==0:
  259.           aface["v"] = (sx1,sy1,0, sx2,sy2,0, sx2,sy2,self.theight)
  260.         else:
  261.           aface["v"] = (sx1,sy1,0, sx2,sy2,self.theight, sx2,sy2,0)
  262.         p.appenditem(aface)
  263.         e.appenditem(p)
  264.     # Drop the items
  265.     quarkpy.mapbtns.dropitemsnow(self.editor, [g], "make x-tree")
  266.  
  267. class MakeQ2XTreeDlg(MakeXTreeDlg):
  268.   size = (300, 150)
  269.   dlgdef = MakeXTreeDlg.dlgdef + """
  270.         sep: = { Typ ="S" Txt=""}
  271.         MakeXTree:py = {Txt="" }
  272.         close:py = {Txt="" }
  273.       }
  274.     """
  275.  
  276.   def MakeFace(self):
  277.     aface = quarkx.newobj("tree:f")
  278.     aface["Contents"] = "402653248"
  279.     aface["Flags"] = "48"
  280.     aface["tex"] = self.tex
  281.     return aface
  282.  
  283.   def MakeXTree(self, btn):
  284.     self.AcceptDialog()
  285.     if not quarkpy.b2utils.iseven(self.planes):
  286.       quarkx.msgbox("Sorry, but in Quake2 you must enter even numbers for 'wings'.",MT_INFORMATION, MB_OK)
  287.       return
  288.     # Create the tree
  289.     g = quarkx.newobj(self.treename+":g");
  290.     ang=pi*2/self.planes
  291.     n=1
  292.     aface = self.MakeFace()
  293.     aface["v"] = (-self.twidth/2,-self.twidth/2,0,
  294.                   -self.twidth/2, self.twidth/2,0,
  295.                    self.twidth/2,-self.twidth/2,0)
  296.     g.appenditem(aface)
  297.     for k in range(self.planes / 2):
  298.       p = quarkx.newobj("poly:p");
  299.       x1 = self.twidth/2 * cos(k*ang)
  300.       y1 = self.twidth/2 * sin(k*ang)
  301.       x2 = n * sin(k*ang)
  302.       y2 = n * cos(k*ang)
  303.       for i in [-1,1]:
  304.         #Create a help plane for texture mapping
  305.         proj = self.MakeFace()
  306.         proj["v"] = (i*-x1,i*-y1,0,
  307.                      i* x1,i* y1,0,
  308.                      i*-x1,i*-y1,self.theight)
  309.         proj["tv"] = (-self.twidth/2,0,
  310.                        -self.twidth / (self.tsize[0] / 128)-self.twidth/2,0,
  311.                        -self.twidth/2, -self.theight / (self.tsize[1] / 128) - 0.5)
  312.         proj["tv"] = (-self.twidth,0,
  313.                       -self.twidth / (self.tsize[0] / 128)-self.twidth,0,
  314.                       -self.twidth, -self.theight / (self.tsize[1] / 128) - 0.5)
  315.         #Create the real brush
  316.         aface = self.MakeFace()
  317.         aface["v"] = (i*x2,i*-y2,0,
  318.                       i*x1,i* y1,0,
  319.                       i*x2,i*-y2,self.theight)
  320.         pface = maptagside.projecttexfrom(proj, aface)
  321.         p.appenditem(pface)
  322.         aface = self.MakeFace()
  323.         aface["v"] = (i*-x2,i*y2,0,
  324.                       i*-x2,i*y2,self.theight,
  325.                       i* x1,i*y1,0)
  326.         pface = maptagside.projecttexfrom(proj, aface)
  327.         p.appenditem(pface)
  328.         aface = self.MakeFace()
  329.         aface["v"] = (i*-x2,i* y2,0,
  330.                       i*-x1,i*-y1,self.theight,
  331.                       i* x1,i* y1,self.theight)
  332.         pface = maptagside.projecttexfrom(proj, aface)
  333.         p.appenditem(pface)
  334.       g.appenditem(p)
  335.     quarkpy.mapbtns.dropitemsnow(self.editor, [g], "make x-tree")
  336.  
  337.  
  338. # Menu click...
  339. def MakeXTreeClick(m):
  340.   editor = mapeditor()
  341.   if editor is None:
  342.     return
  343.   game = quarkx.setupsubset().shortname
  344.   if game == 'Half-Life':
  345.     MakeHLXTreeDlg(quarkx.clickform, editor)
  346.   else:
  347.     if game == 'Quake 2':
  348.       MakeQ2XTreeDlg(quarkx.clickform, editor)
  349.     else:
  350.       if game == 'Sin':
  351.         MakeSinXTreeDlg(quarkx.clickform, editor)
  352.         quarkx.msgbox("When I coded the plug-in I did not have Sin. I still need someone to test it, or tell me if I did something wrong. I would be glad if you could send me a mail to Marco.Leise@gmx.de!",MT_INFORMATION, MB_OK)
  353.       else:
  354.         if game in ['Quake 1','Heretic II', 'Hexen II']:
  355.           quarkx.msgbox("This plugin works with masked textures. "+game+" does not support this kind of transparency.",MT_INFORMATION, MB_OK)
  356.         else:
  357.           quarkx.msgbox(game+" is currently not supported, sorry. Please post a mail to Marco.Leise@gmx.de and I'll see what I can do.",MT_INFORMATION, MB_OK)
  358.  
  359. # in case the developer (that would be me) reloaded the plugin, delete the previous button procedure
  360. for m in quarkpy.mapcommands.items:
  361.   if m <> None:
  362.     if m.text == "Make &X-Tree":
  363.       quarkpy.mapcommands.items.remove(m)      
  364. # Register the plug-in
  365. quarkpy.mapcommands.items.append(quarkpy.qmenu.item("Make &X-Tree", MakeXTreeClick))